package com.springbootshiyanyi.demo.service;
import com.springbootshiyanyi.demo.entity.Student;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentService {
    private List<Student> students = new ArrayList<>();
    public StudentService() {
        // 初始化一些学生数据
        students.add(new Student(1L, "ShaoJu", "SJ", "ShaoJu.SJ@example.com", 23));
        students.add(new Student(2L, "GengFang", "GF", "GengFang.GF@example.com", 23));
        students.add(new Student(3L, "zhangSan", "ZS", "GengFang.GF@example.com", 22));
        students.add(new Student(4L, "LiSi", "LS", "GengFang.GF@example.com", 21));
    }
    public List<Student> findAllStudents() {
        return students;
    }
    public Student createStudent(Student student) {
        student.setId((long) (students.size() + 1));
        students.add(student);
        return student;
    }
    public Student getStudentById(Long id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student; } }
        return null;
    }
    public Student updateStudent(Long id, Student updatedStudent) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {
                updatedStudent.setId(id);
                students.set(i, updatedStudent);
                return updatedStudent;
            }
        }
        return null;
    }
    public boolean deleteStudent(Long id) {
        return students.removeIf(student -> student.getId().equals(id));
    }

    // 其他CRUD操作的方法
}

